package com.lazybones.base.engineer.service.mail.impl;

import com.lazybones.base.engineer.common.enums.StatusEnum;
import com.lazybones.base.engineer.common.exception.LazyBonesException;
import com.lazybones.base.engineer.common.res.ResultRes;
import com.lazybones.base.engineer.service.mail.IMailService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.Objects;

@Component
public class MailServiceImpl implements IMailService {

    private final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
    @Value("${spring.mail.username}")
    private String from;
    private final JavaMailSender mailSender;
    public MailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void sendText(String to, String subject, String text) {
        //发送简单邮件方法
        SimpleMailMessage simpleMail = new SimpleMailMessage();
        checkMail(to,subject,text);
        simpleMail.setText(text);
        simpleMail.setTo(to);
        simpleMail.setSubject(subject);
        simpleMail.setFrom(from);
        simpleMail.setSentDate(new Date());
        simpleMail.setCc("wen123103624@126.com");
        try {
            mailSender.send(simpleMail);
            log.info("邮件已成功发送！~收件人是：{}",to);
        }catch (Exception e){
            throw new LazyBonesException(StatusEnum.SERVICE_ERROR);
        }
    }

    @Override
    public void sendHtml(String to, String subject, String text) {
        checkMail(to,subject,text);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSentDate(new Date());
            helper.setSubject(subject);
            helper.setText(text,true);
            mailSender.send(message);
        } catch (MessagingException e) {
            throw new LazyBonesException(StatusEnum.SERVICE_ERROR);
        }


    }

    @Override
    public void sendMimeMessages(String to, String subject, String  mimeMessages,String files) {
        checkMail(to,subject,mimeMessages);
        String[] toCc = new String[]{"wen123103624@126.com","123103624@qq.com"};
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper =  new MimeMessageHelper(message, true);
            helper.setText("<html><body><img src='cid:image123'>" +
                    "</body></html>"+mimeMessages, true);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setTo(to);
            helper.setCc(toCc);
            FileSystemResource res = new FileSystemResource(new File(files));
            helper.addInline("image123",res);
            FileSystemResource file = null;
            if(!files.isEmpty()) {
                file = new FileSystemResource(files);
                helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            throw new LazyBonesException(StatusEnum.SERVICE_ERROR);
        }
    }
    private void checkMail(String to, String subject, String text){
        if(StringUtils.isBlank(to)){
            throw new LazyBonesException(StatusEnum.PARAM_Null);
        }
        if(StringUtils.isBlank(subject)){
            throw new LazyBonesException(StatusEnum.PARAM_Null);
        }
        if(StringUtils.isBlank(text)){
            throw new LazyBonesException(StatusEnum.PARAM_Null);
        }
    }
}
